package com.easyadmin.pro.common.initutils;

import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.modules.sys.entity.UserEntity;
import com.easyadmin.pro.modules.sys.service.IUserService;
import com.easyadmin.pro.tool.RedisUtils;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * packageName com.easyadmin.pro.common.initutils
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className InitUserInfo
 * @date 2024/8/21
 * @description 初始化用户信息
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InitUserInfo {

    private final IUserService userService;

    private final RedisTemplate redisTemplate;

    private final String USER_INFO_REDIS_KEY = "user-info-map";

    @PostConstruct
    public void generator() {
        List<UserEntity> userInfos = userService.list(new QueryWrapper()
                .eq(UserEntity::getStatus, StatusConstants.STATUS_1));
        Map<String, String> userInfoMap = userInfos.stream()
                .collect(Collectors.toMap(e -> e.getId().toString(), UserEntity::getTrueName));
        redisTemplate.opsForHash().putAll(USER_INFO_REDIS_KEY, userInfoMap);
    }
}